package com.ui.design.view.waterviewexpend.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;

import com.ui.design.R;

/**
 * 两层
 * 圆形波纹+扩散效果
 * tangxianfeng
 * 2021.7.21
 */
public class CircleWaveView extends android.view.View {

    //画笔(第一个波纹)
    private Paint mpaint;
    //透明度
    private Integer alpha = 255;
    //扩散颜色
    private int mColor;
    //圆半径
    private float mCoreRadius = 0;
    //扩散动画控制
    private ValueAnimator valueAnimator;
    //画笔(第二个波纹)
    private Paint mpaint2;
    //透明度(第二个波纹)
    private Integer alpha2 = 255;
    //圆半径(第二个波纹)
    private float mCoreRadius2 = 0;
    //扩散动画控制(第二个波纹)
    private ValueAnimator valueAnimator2;
    //控制变量
    private boolean control = false;
    private int maxRaduis = 40;
    private int minRaduis = 20;


    public CircleWaveView(Context context, @androidx.annotation.Nullable AttributeSet attrs) {
        super(context, attrs);
        //也可通过attr进行xml文件内的自定义设置
        if (attrs != null) {
            @SuppressLint({"Recycle", "CustomViewStyleable"}) TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCircleWave);
            if (typedArray.getString(R.styleable.MyCircleWave_MyCircleWaveColor) == null) {
                //获取自定义属性（默认设置）
                mColor = Color.WHITE;
            } else {
                mColor = Color.parseColor(typedArray.getString(R.styleable.MyCircleWave_MyCircleWaveColor));
            }
            Log.e("BRaduis", maxRaduis + ":" + minRaduis);
            maxRaduis = (int) typedArray.getDimension(R.styleable.MyCircleWave_MyCircleWaveMaxRadius, 40);
            minRaduis = (int) typedArray.getDimension(R.styleable.MyCircleWave_MyCircleWaveMinRadius, 20);
        }
        init();
    }


    private void init() {
        mpaint = new Paint();
        mpaint2 = new Paint();
        mpaint.setColor(mColor);
        mpaint2.setColor(mColor);
        valueAnimator2 = ValueAnimator.ofInt(minRaduis, maxRaduis);
        valueAnimator2.setDuration(3000);
        valueAnimator2.addUpdateListener(animation -> {
            alpha2 = maxRaduis - (int) animation.getAnimatedValue();
            mCoreRadius2 = (int) animation.getAnimatedValue();
        });
        valueAnimator2.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator2.setRepeatMode(ValueAnimator.RESTART);

        valueAnimator = ValueAnimator.ofInt(minRaduis, maxRaduis);
        valueAnimator.setDuration(3000);
        valueAnimator.addUpdateListener(animation -> {
            alpha = maxRaduis - (int) animation.getAnimatedValue();
            mCoreRadius = (int) animation.getAnimatedValue();
            invalidate();
        });
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);

    }


    /**
     * 进行圆形重绘
     *
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制扩散圆
        mpaint.setAlpha(alpha);
        mpaint2.setAlpha(alpha2);
        canvas.drawCircle((float) getWidth() / 2, (float) getWidth() / 2, mCoreRadius, mpaint);
        canvas.drawCircle((float) getWidth() / 2, (float) getWidth() / 2, mCoreRadius2, mpaint2);
    }

    public void setRadius(int maxRaduis, int minRaduis) {
        this.maxRaduis = maxRaduis;
        this.minRaduis = minRaduis;

    }

    /**
     * 开始扩散
     */
    public void start() {
        if (!control) {
            valueAnimator.start();
            valueAnimator2.setStartDelay(1300);
            valueAnimator2.start();
            control = true;
        }
        invalidate();
    }

    public boolean getState() {
        return control;
    }

    /**
     * 停止扩散
     */
    public void stop() {
        if (control) {
            valueAnimator.pause();
            valueAnimator2.pause();
        }
        control = false;
        alpha = 0;
        alpha2 = 0;
        mCoreRadius = 0;
        mCoreRadius2 = 0;
        invalidate();
    }
}


